<?php

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
 * This file contains some helper functions for creating portable URLs. The
 * following functions are defined:
 *
 * - base_url
 * - base_file
 * - link_to
 * - anchor
 *
 * PHP version 5
 *
 * This file is part of Majic - A rapid-development web application framework
 * for PHP 5.
 *
 * Majic is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * Majic is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Majic. If not, see <http://www.gnu.org/licenses/>.
 *
 * @package     Majic
 * @author      Michael J. I. Jackson <mjijackson@gmail.com>
 * @copyright   2007-2008 Michael J. I. Jackson
 * @license     http://www.gnu.org/licenses/lgpl-3.0.txt GNU LGPL 3.0
 * @version     SVN: $Id: url.php 45 2008-11-22 20:50:54Z mjijackson $
 */

/**
 * Gets the base URL of the site as defined in the config file.
 *
 * @param   bool    $return     True to return a string, false to echo
 * @return  mixed               The base URL as a string or void
 */
function base_url($return = false)
{
    $base_url = Majic::getConfig('base_url');

    if ($return) {
        return $base_url;
    } else {
        echo $base_url;
    }
}

/**
 * Gets the URL of the index page.
 *
 * @param   bool    $return     True to return a string, false to echo
 * @return  mixed               The base file URL as a string or void
 */
function base_file($return = false)
{
    $base_file = base_url(true) . Majic::getConfig('base_file');

    if ($return) {
        return $base_file;
    } else {
        echo $base_file;
    }
}

/**
 * Creates a link to another page of the site.
 *
 * @param   string  $page       The controller/method/param to link to
 * @param   bool    $return     True to return a string, false to echo
 * @return  string              The href to the link
 */
function link_to($page = '', $return = false)
{
    $base_url = Majic::getConfig('base_url');
    $base_file = Majic::getConfig('base_file');

    $link = $base_url . $base_file;
    if (!empty($page)) {
        $ext = Majic::getConfig('extension');
        if ($ext != '') {
            $page .= ".$ext";
        }
        if ($base_file != '') {
            $link .= '/';
        }
        $link .= $page;
    }

    if ($return) {
        return $link;
    } else {
        echo $link;
    }
}

/**
 * Creates a link to another page of the site.
 *
 * @param   string  $page       The page to link to
 * @param   string  $text       The text of the link
 * @param   string  $title      (optional) The title of the link
 * @param   bool    $return     True to return a string, false to echo
 * @return  mixed               The link's href as a string or void
 */
function anchor($page, $text, $title = null, $return = false)
{
    $anchor = '<a href="' . link_to($page, true) . '"';
    if ($title != null) {
        $anchor .= " title=\"$title\"";
    }
    $anchor .= ">$text</a>";

    if ($return) {
        return $anchor;
    } else {
        echo $anchor;
    }
}

?>
